<?php
require('includes/application_top.php');
require('includes/classes/http_client.php');

global $customer_id;

$ip = $_SERVER['REMOTE_ADDR'];

// List points from database
if ($_GET['action'] == 'listpoints') {
    $map_query = tep_db_query("select * from address_book where customers_id = '$customer_id'");
    $points = array();
    while ($row = tep_db_fetch_array($map_query)) {
        $countries_id = $row['entry_country_id'];
        $countries_query = tep_db_query("select * from countries where countries_id = '$customer_id'");
        $countries = tep_db_fetch_array($countries_query);
        
        $name = $row['entry_firstname'].' '.$row['entry_lastname'];
        $address = $row['entry_street_address'].' Phường '.$row['entry_suburb'].' Quận '.$row['entry_state'].' '.$row['entry_city'].' '.$countries['countries_name'];
        
        //echo $name.$address; die;
        array_push($points, array('name' => $name, 'address' => $address));
    }
    
    $map_query2 = tep_db_query("select * from ushop_googlemap where customers_id = '$customer_id'");
    while ($row2 = tep_db_fetch_array($map_query2)) {
        $countries_id = $row['entry_country_id'];
        $countries_query = tep_db_query("select * from countries where countries_id = '$customer_id'");
        $countries = tep_db_fetch_array($countries_query);
        
        $name2 = $row2['entry_firstname'].' '.$row2['entry_lastname'];
        $address2 = $row2['entry_street_address'].' Phường '.$row2['entry_suburb'].' Quận '.$row2['entry_state'].' '.$row2['entry_city'].' '.$countries2['countries_name'];
        
        //echo $name.$address; die;
        array_push($points, array('name' => $name2, 'address' => $address2));
    }
    
    echo json_encode(array("Locations" => $points));
    exit;
}

// Save a point from our form
if ($_POST['action'] == 'savepoint') {
    $name = $_POST['name'];
    /*if(preg_match('/[^\w\s]/i', $name)) {
        fail('Invalid name provided.');
    }*/
    if(empty($name)) {
        fail('Please enter a name.');
    }

    // Query
    $result = tep_db_query("UPDATE address_book SET lat='$_POST[lat]', lng='$_POST[lng]', ip='$ip' WHERE customers_id='$customer_id'");
    if ($result) {
        success(array('lat' => $_POST['lat'], 'lng' => $_POST['lng'], 'name' => $name));
    } else {
        fail('Failed to add point.');
    }
    exit;
}

if ($_POST['action'] == 'savepoint_2') {
    $entry_lastname = $_POST['entry_lastname'];
    /*if(preg_match('/[^\w\s]/i', $name)) {
        fail('Invalid name provided.');
    }*/
    if(empty($entry_lastname)) {
        fail('Please enter a name.');
    }

    // Query
    $result = tep_db_query("INSERT INTO ushop_googlemap SET customers_id='$customer_id', entry_lastname='$_POST[entry_lastname]', entry_street_address='$_POST[entry_street_address]', entry_suburb='$_POST[entry_suburb]', entry_state='$_POST[entry_state]', entry_city='$_POST[entry_city]', lat='$_POST[lat]', lng='$_POST[lng]', ip='$ip'");
    if ($result) {
        success(array('lat' => $_POST['lat'], 'lng' => $_POST['lng'], 'name' => $entry_lastname));
    } else {
        fail('Failed to add point.');
    }
    exit;
}

function map_query($query) {
    // Connect
    mysql_connect('localhost', 'root', '')
    OR die(fail('Could not connect to database.'));
    
    mysql_select_db('toiuu_ushop_db');
    return mysql_query($query);
}
    
function fail($message) {
    die(json_encode(array('status' => 'fail', 'message' => $message)));
}

function success($data) {
    die(json_encode(array('status' => 'success', 'data' => $data)));
}
?>